package com.auth.sercurity;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;

/**
 * @author 大忽悠
 * @create 2023/2/9 15:03
 */
public class DaoTokenAuthenticationFilter extends AbstractTokenAuthenticationFilter{
    private final UserDetailsService userDetailsService;

    public DaoTokenAuthenticationFilter(UserDetailsService userDetailsService) {
        this.userDetailsService = userDetailsService;
    }

    /**
     * 对当前用户状态进行检查,如果状态异常,返回false,否则返回true
     *
     * @param userDetails
     * @return
     */
    @Override
    protected boolean userStatusCheck(UserDetails userDetails) {
        return userDetails.isEnabled();
    }

    /**
     * @param token 待验证令牌
     * @return 通过解析token来获取当前用户详细信息
     */
    @Override
    protected UserDetails verifyToken(String token) {
        String username = jwt.validateToken(token);
        return userDetailsService.loadUserByUsername(username);
    }
}
